{extend name='table'}

{block name="button"}

<button  data-modal='{:url("add")}' 
  class='layui-btn layui-btn-sm layui-btn-primary'>{:lang('添加')}</button>

{/block}

{block name="content"}
<div class="think-box-shadow">
  {include file='stock_good/index_search' /}
  <table id="OplogTable" data-url="{:request()->url()}" data-target-search="form.form-search"></table>
</div>
{/block}

{block name='script'}
<script>
  $(function () {
    $('#OplogTable').layTable({
      even: false, height: 'full',
      sort: { field: 'id', type: 'asc' },
      cols: [[
        {
          field: 'remaining', title: '名称', width: 150, align: 'center', templet: function (d) {
            if (d.remaining - d.threshold <= 0) return '<span class="layui-badge layui-bg-orange">' + d.name +'</span>';
            else return '<span class="layui-badge layui-bg-green">'+d.name+'</span>';
          }
        },
        { field: 'total', width: 100, title: '总库存(kg)', align: 'center' },
        { field: 'used', width: 100,  title: '已用库存', align: 'center' },
        {
          field: 'remaining',  title: '剩余库存', width: 100, align: 'center'
        },

        {
          field: 'type', title: '剩余件数', width: 100, align: 'center', templet: function (d) {
            return '<span>' + parseInt(d.remaining / d.bulk_weight) + '件</span>';
          }
        },
        {
          field: 'type', title: '预警阈值', width: 100, align: 'center', templet: function (d) {
            return '<span>' + d.threshold + 'kg</span>';
          }
        },

        // { field: 'status', title: '{:lang("是否关联")}', align: 'center', width: 110, templet: '#StatusSwitchTpl' },
        { title: '操作',  align: 'center', toolbar: '#toolbar' }
      ]]
    });
  });

  // 数据状态切换操作
    layui.form.on('switch(StatusSwitch)', function (obj) {
      var data = { id: obj.value, status: obj.elem.checked > 0 ? 1 : 0 };
      $.form.load("{:url('state')}", data, 'post', function (ret) {
        if (ret.code < 1) $.msg.error(ret.info, 3, function () {
          $('#OplogTable').trigger('reload');
        });
        return false;
      }, false);
    });
</script>

<!-- 数据状态切换模板 -->
<script type="text/html" id="StatusSwitchTpl">
    <!--{if auth("state")}-->
  <input type="checkbox" value="{{d.id}}" lay-skin="switch" lay-text="{:lang('是')}|{:lang('否')}"
  lay-filter="StatusSwitch" {{-d.status>0?'checked':''}}>
  <!--{else}-->
  {{-d.status ? '<b class="color-green">{:lang("是")}</b>' : '<b class="color-red">{:lang("否")}</b>'}}
  <!--{/if}-->
</script>


<script type="text/html" id="toolbar">
  <!--{if auth("inbound")}-->
  <a class="layui-btn layui-btn-sm" data-event-dbclick data-title="{:lang('入库')}"
    data-modal='{:url("inbound")}?id={{d.id}}'>{:lang('入库')}</a>
  <!--{/if}-->
  <!--{if auth("outbound")}-->
  <a class="layui-btn layui-btn-sm" data-event-dbclick data-title="{:lang('出库')}"
    data-modal='{:url("outbound")}?id={{d.id}}'>{:lang('出库')}</a>
  <!--{/if}-->
  <!--{if auth("logs")}-->
  <a class="layui-btn layui-btn-sm" data-event-dbclick data-title="{:lang('流水')}"
    data-open='{:url("logs")}?goods_id={{d.id}}'>{:lang('流水')}</a>
  <!--{/if}-->
    <!--{if auth("edit")}-->
    <a class="layui-btn layui-btn-sm" data-event-dbclick data-title="{:lang('编辑')}"
      data-modal='{:url("edit")}?id={{d.id}}'>{:lang('编辑')}</a>
    <!--{/if}-->
   
</script>
{/block}